import requests
import json
# import openai

# import os

# api_key = "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"


# api_key = "sk-Oi44RkFNKYaTt7kJTdHt6OeparnIQh6MZ389gNuvuo1EFoLO"
# api_url = "https://gpt.soruxgpt.com/api/api/v1/chat/completions"

api_key = "sk-Oi44RkFNKYaTt7kJTdHt6OeparnIQh6MZ389gNuvuo1EFoLO"
api_url = "https://gpt.soruxgpt.com/api/api/v1/chat/completions"
# api_url = "https://proxy.soruxgpt.com/api/api/v1/chat/completions"
# organization = "org-3GmIddedkgXNruiRIrjTf2UC"
# def myapi(que, inf = None):
#     os.environ['REQUESTS_CA_BUNDLE'] = 'certificate.crt'
#     openai.organization = organization
#     openai.api_key = api_key
#
#     completion = openai.ChatCompletion.create(
#         model="gpt-3.5-turbo",
#         messages=[
#             {"role": "user", "content": "Hello"}
#         ]
#     )
#     print(completion.choices[0].message.content)
def askGPT(que, inf = None):
    print(inf)
    # 只查询一次。
    if inf is not None:
        prompt = que + "根据以下信息，用完整的一段话回答问题，但是不要回复多余的东西（如“根据所给信息”）：" + inf + "如果根据所给信息无法回答该问题，请回复一个空格。"
    else:
        prompt = que
    headers = {
        "Authorization": "Bearer " + api_key,
        "Content-Type": "application/json"
    }
    print(prompt)
    data = {
        "messages": [
            {
                "role": "user",
                "content": prompt
            }
        ],
        "model": "gpt-4"
    }
    print(data)
    response = requests.post(api_url, headers=headers, data=json.dumps(data))
    print("response is: ", end=" ")
    print(response.text)
    response_data = json.loads(response.text)
    if response.status_code == 200:
        choices = response_data["choices"]
        for choice in choices:
            message = choice["message"]
            content = message["content"]
            return content
    else:
        return str(response.status_code)
            # "error"

def askGPT_combine(que, inf = None):
    print(inf)
    prompt = que + "将以下信息整合成一段话，简短地回答以下问题，请记住不要回复多余的内容：" + inf + "如果根据所给信息无法回答该问题，请回复：抱歉，我并不了解这个问题，请咨询医生寻求解答。"
    # api_url = "https://gpt.soruxgpt.com/api/api/v1/chat/completions"
    headers = {
        "Authorization": "Bearer " + api_key,
        "Content-Type": "application/json"
    }
    data = {
        "messages": [
            {
                "role": "user",
                "content": prompt
            }
        ],
        "model": "gpt-3.5-turbo"
    }

    response = requests.post(api_url, headers=headers, data=json.dumps(data))
    print(response.text)
    response_data = json.loads(response.text)
    if response.status_code == 200:
        choices = response_data["choices"]
        for choice in choices:
            message = choice["message"]
            content = message["content"]
            return content
    else:
        return "error"

def ask_anything(que):
    prompt = que
    headers = {
        "Authorization": "Bearer " + api_key,
        "Content-Type": "application/json"
    }
    print(prompt)
    data = {
        "messages": [
            {
                "role": "user",
                "content": prompt
            }
        ],
        "model": "gpt-4"
    }
    print(data)
    response = requests.post(api_url, headers=headers, data=json.dumps(data))
    # print("response is: ", end=" ")
    print(response.text)
    response_data = json.loads(response.text)
    if response.status_code == 200:
        choices = response_data["choices"]
        for choice in choices:
            message = choice["message"]
            content = message["content"]
            return content
    else:
        return str(response.status_code)
        # "error"